﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq.Expressions;

namespace IceDog.DNF.Program.Dapper
{
    public class DBManager
    {
        private string _connectionString;

        public DBManager()
        {
            _connectionString=String.Empty;
        }
        /// <summary>
        /// 连接字符串
        /// </summary>
        public string ConnectionString
        {
            get
            {
                if (string.IsNullOrEmpty(_connectionString))
                {
                    var sb = new SqlConnectionStringBuilder
                    {
                        //DataSource = ".",
                        DataSource = "192.168.0.24",
                        InitialCatalog = "test",
                        IntegratedSecurity = false,
                        MultipleActiveResultSets = true,
                        UserID = "sa",
                        Password = "edoc2",

                    };
                    _connectionString = sb.ToString();
                    return _connectionString;
                }

                return _connectionString;
            }
        }
        public IDbConnection DbConnection=>new SqlConnection(ConnectionString);
        /// <summary>
        /// 获取属性名
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expr"></param>
        /// <returns></returns>
        public string GetPropertyName<T>(Expression<Func<T, object>> expr)
        {
            var rtn = "";
            if (expr.Body is UnaryExpression)
            {
                rtn = ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;
            }
            else if (expr.Body is MemberExpression)
            {
                rtn = ((MemberExpression)expr.Body).Member.Name;
            }
            else if (expr.Body is ParameterExpression)
            {
                rtn = ((ParameterExpression)expr.Body).Type.Name;
            }
            return rtn;
        }
    }
}
